$(function() {
	//remove disabled property from fields in office template 
	$("#office-temp input,#office-temp textarea").prop("disabled",false);
	$(".category-accordion").each(function(){
		$(this).accordion({ active: false, collapsible: true,  heightStyle: "content",icons: { "header": "ui-icon-plus", "activeHeader": "ui-icon-minus" }});
	});
	
	//update count for selected categories.
	$(".ksr .category-accordion input[type='checkbox']").click(function(){
		countSelectedCategories();
	})
	countSelectedCategories();
  });
//count selected categories
function countSelectedCategories(){
	$(".ksr .category-accordion label.non-leaf").each(function(){
		var categoryId=$(this).attr("category");
		$(this).find("span.count").text($(this).siblings("div.content[category="+categoryId+"]").find("input[type='checkbox']:checked").length);
	})
}
//switch view/edit mode in my-profile page
$("#business-profile-update input[type='radio'][name='mode']").click(function(){
	if ($(this).val()=='view'){
		$("form[name='ksr-business-signup-form']").removeClass("edit");
		$("form[name='ksr-business-signup-form']").addClass("view");
		$("form[name='ksr-business-signup-form'] input,form[name='ksr-business-signup-form'] select,form[name='ksr-business-signup-form'] textarea").prop("disabled",true);
		$("form[name='ksr-business-signup-form'] label.error").hide()
		disableDropzone();
	} else {
		$("form[name='ksr-business-signup-form']").removeClass("view");
		$("form[name='ksr-business-signup-form']").addClass("edit");
		$("form[name='ksr-business-signup-form'] label.error").show()
		$("form[name='ksr-business-signup-form'] input,form[name='ksr-business-signup-form'] select,form[name='ksr-business-signup-form'] textarea").prop("disabled",false);
		enableDropzone();
	}
})
$("#add-an-office").click(function(){
	$('#office-temp .office-wrapper').clone().prependTo('#office-list');
	$("#office-list .office-wrapper:hidden").first().show({effect:"blind",direction:"up"},1,function(){
		resetOfficeList();
		addRemoveOfficeEvent();
	});
});
function addRemoveOfficeEvent(){
	$("#office-list .office-wrapper").each(function(){
		var that=this;
		$(that).find(".trash").unbind("click");
		$(that).find(".trash").click(function(){
			var self=this;
			var removeId=$(self).attr("ref-id");
			$(removeId).hide({effect:"blind",direction:"up"},1000,function(){
				$(removeId).parents(".office-wrapper").remove();
				resetOfficeList();
			});
		});
	});
}
addRemoveOfficeEvent();
function resetOfficeList(){
	$("#office-list .office-wrapper .office").each(function(idx,ele){
		$(ele).attr("id","office-"+idx);
		$(ele).find(".trash").attr("ref-id","#office-"+idx);
		$(ele).find(".office-title").html("Office "+(idx+1));
		$(ele).find(".errContainer label").each(function(){
			var indexOfLastDash=$(this).attr("for").lastIndexOf("-")
			var orignalValue=$(this).attr("for").substring(0,indexOfLastDash);
			$(this).attr("for",orignalValue+"-"+idx);
		})
		$(ele).find("input[id!='']").each(function(){
			var indexOfLastDash=$(this).attr("id").lastIndexOf("-")
			var orignalValue=$(this).attr("id").substring(0,indexOfLastDash);
			$(this).attr("id",orignalValue+"-"+idx);
		})
		$(ele).find("input[name^='offices']").each(function(){
			var indexOfFirstDot=$(this).attr("name").indexOf(".")
			var nameSuffix = $(this).attr("name").substring(indexOfFirstDot);
			var replacement="offices["+idx+"]";
			$(this).attr("name",replacement+nameSuffix);
		})
		$(ele).find("textarea[name^='offices']").each(function(){
			var indexOfFirstDot=$(this).attr("name").indexOf(".")
			var nameSuffix = $(this).attr("name").substring(indexOfFirstDot);
			var replacement="offices["+idx+"]";
			$(this).attr("name",replacement+nameSuffix);
		})
	});
	if ($(".form .office .trash").length>1){
		$(".form .office .trash").removeClass("ksr-hide");
	} else{
		$(".form .office .trash").addClass("ksr-hide");
	}
	$(".ksr .total-offices .count").text($("#office-list .office-wrapper").length);
}


  
	$(".ksr form[name='ksr-business-signup-form']").validate({
		ignore:"",
		errorContainer:	(".ksr form[name='ksr-business-signup-form'] div.errContainer" ),
		rules:{
			categories:"required"
		},
		messages:{
			"businessName":"Missing name.",
			"businessType":"Missing type.",
			"categories":"Missing categories.",
			"emailAddress":{
				required:"Missing email.",
				email:"Invalid email",
				remote:"This email already exists."
			},
			password:"Missing password.",
			password2:{
				required:"Missing confirmed password.",
				equalTo:"Password mismatch."
			},
			"phoneNumber":"Missing phone number.",
			"specialty":"Missing specialty."
		}	
	});
	
	$(".ksr form[name='ksr-business-login-form']").validate({
		errorContainer:	(".ksr form[name='ksr-business-login-form'] div.errContainer" ),
		messages:{
			"email":{
				required:"Missing email.",
				email:"Invalid email"
			},
			password:"Missing password."
		}	
	});
